package quick.api.app.service.cdn;


import com.alibaba.fastjson.JSONObject;
import com.tencentcloudapi.cdn.v20180606.CdnClient;
import com.tencentcloudapi.cdn.v20180606.models.*;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.vod.v20180717.models.PushUrlCacheResponse;
import org.apache.commons.lang3.StringUtils;
import webdm.business.bean.Context;
import webdm.business.bean.ResponseDTO;
import webdm.business.component.AbstractPipelineComponent;
import webdm.business.util.ValueUtil;

import java.util.List;


public class TencentCdn extends AbstractPipelineComponent {

    private CdnClient client;
    @Override
    public void setConfig(JSONObject config) {
        super.setConfig(config);

        String secretId = ValueUtil.getValue(config, "secret_id", null);
        String secretKey = ValueUtil.getValue(config, "secret_key", null);
        if (StringUtils.isNotBlank(secretId) && StringUtils.isNotBlank(secretKey)) {
            Credential cred = new Credential(secretId, secretKey);
            this.client = new CdnClient(cred, "ap-shanghai");
        }
    }

    /**
     * 刷新文件
     * @param urls
     * @return
     * @throws TencentCloudSDKException
     */
    private ResponseDTO purgeUrlsCache(String urls[]) throws TencentCloudSDKException {

        PurgeUrlsCacheRequest request = new PurgeUrlsCacheRequest();
        request.setUrlEncode(true);
        request.setUrls(urls);
        PurgeUrlsCacheResponse response = client.PurgeUrlsCache(request);
        return ResponseDTO.getSuccessInstance(response.getTaskId());
    }

    /**
     * 刷新目录
     * @param context
     * @param urls
     * @return
     * @throws TencentCloudSDKException
     */
    private ResponseDTO purgePathsCache(Context context, String urls[]) throws TencentCloudSDKException {

        String type = context.get("param.type", "flush");
        PurgePathCacheRequest request = new PurgePathCacheRequest();
        request.setPaths(urls);
        request.setUrlEncode(true);
        request.setFlushType(type);
        PurgePathCacheResponse response = client.PurgePathCache(request);
        return ResponseDTO.getSuccessInstance(response.getTaskId());
    }

    private ResponseDTO pushUrlCache(String urls[]) throws TencentCloudSDKException {

        PushUrlsCacheRequest request = new PushUrlsCacheRequest();
        request.setUrls(urls);
        PushUrlsCacheResponse response = client.PushUrlsCache(request);
        return ResponseDTO.getSuccessInstance(response.getRequestId());
    }

    @Override
    public ResponseDTO process(Context context) {
        String action = context.get("param.action", "refresh_urls");

        // 处理url
        Object urlObject = context.get("param.urls");
        if (urlObject == null) {
            return ResponseDTO.getFailInstance();
        }
        String []urlArr = null;
        if (urlObject instanceof String ) {
            urlArr = new String[]{(String)urlObject};
        } else if (urlObject instanceof List) {
            List list = ((List)urlObject);
            if(list.size() > 0) {
                urlArr = new String[list.size()];
                list.toArray(urlArr);
            }

        }
        if (urlArr == null) {
            return ResponseDTO.getFailInstance();
        }

        try{
            if("refresh_urls".equals(action)) {
                return this.purgeUrlsCache(urlArr);
            }else if("refresh_paths".equals(action)){
                return this.purgePathsCache(context, urlArr);
            }else if("reload_urls".equals(action)){
                return this.pushUrlCache(urlArr);
            }else {
                return ResponseDTO.getFailInstance("没有对应的处理方法");
            }
        }catch (Exception e){
            return ResponseDTO.getFailInstance(e.toString());
        }
    }
}
